<template>
  <van-button
    v-if="isfollow"
    class="follow-btn"
    round
    size="small"
    :loading='loading'
    @click="onFollow"
  >已关注</van-button>
  <van-button
    v-else
    class="follow-btn"
    type="info"
    color="#3296fa"
    round
    size="small"
    icon="plus"
    :loading='loading'
    @click="onFollow"
  >关注</van-button>

</template>

<script>
import { addFollow, deleteFollow } from '@/api/user'
export default {
  name: 'FollowUser',
  components: {},
  model: {
    prop: 'isfollow', // 默认是 value
    event: 'update-is_followed' // 默认是 input
  },
  props: {
    isfollow: {
      type: Boolean,
      required: true
    },
    userId: {
      type: [String, Number, Object],
      required: true
    }
  },
  data () {
    return {
      loading: false
    }
  },
  computed: {},
  watch: {},
  created () { },
  methods: {
    async onFollow () {
      this.loading = true
      try {
        if (this.isfollow) {
          // 已关注 取消关注
          await deleteFollow(this.userId)
          // this.article.is_followed = false
        } else {
          // 没关注 要关注
          await addFollow(this.userId)
          // this.article.is_followed = true
        }
        // this.article.is_followed = !this.article.is_followed
        // this.$emit('update-is_followed', !this.value)
        this.$emit('update-is_followed', !this.isfollow)
      } catch (err) {
        let message = '添加关注失败'
        if (err.response && err.response.status === 400) {
          message = '你不能关注你自己'
        }
        this.$toast(message)
      }
      this.loading = false
    }
  }
}
</script>

<style scoped>
</style>
